package com.woniuxy.third.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.third.annotation.RequirePermission;
import com.woniuxy.third.entity.*;
import com.woniuxy.third.mapper.TeacherMapper;
import com.woniuxy.third.entity.Chapter;
import com.woniuxy.third.entity.Course;
import com.woniuxy.third.entity.Teacher;
import com.woniuxy.third.entity.User;
import com.woniuxy.third.model.CourseQuery;
import com.woniuxy.third.result.ResponseResult;
import com.woniuxy.third.service.ChapterService;
import com.woniuxy.third.service.CourseService;
import com.woniuxy.third.service.TeacherService;
import com.woniuxy.third.utils.GetUserUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Resource
    private CourseService courseService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private ChapterService chapterService;

    //获取所有课程，封装结果返回
    @GetMapping("/all")
    public ResponseResult<List<Course>> findAllcourse(){
        List<Course> allCourse = courseService.findAllCourse();
        ResponseResult<List<Course>> responseResult=new ResponseResult<>();
        if(allCourse!=null) {
            responseResult.setCode(200);
            responseResult.setMessage("success");
            responseResult.setData(allCourse);
            return responseResult;
        }else{
            responseResult.setCode(500);
            responseResult.setMessage("false");
            return responseResult;
        }
    }

    /*教师端增加课程
    * 注意数据准确性，避免外键关联错误、非空数据错误
    * */
    @RequirePermission("course:add")
    @PostMapping("/addcourse")
    public ResponseResult<Course> addCourse(@RequestBody Course course, HttpServletRequest request){
        User user = GetUserUtil.getUser(request);
        if(course.getPrice().compareTo(BigDecimal.ZERO)==1){
            course.setIsFree("是");
        }else {
            course.setIsFree("否");
        }
        Teacher teacher = teacherService.findById(user.getId());
        int schoolid = teacher.getSchoolid();
        course.setSchoolid(schoolid);
        course.setTeacherid(user.getId());
        boolean flag=courseService.addCourse(course);
        if(flag){
            return new ResponseResult<>(200,"课程添加成功",course);
        }else {
            return new ResponseResult<>(500,"课程添加失败，请重试",course);
        }
    }

    /*
    教师端修改课程
    */
    @RequestMapping("/updatecourse")
    public ResponseResult<Integer> updateCourse(@RequestBody Course course,HttpServletRequest request){
        User user =GetUserUtil.getUser(request);
        course.setTeacherid(user.getId());
        int i = courseService.updateCourse(course);
        ResponseResult<Integer> responseResult=new ResponseResult<>();
        if(i > 0){
            responseResult.setCode(200);
            responseResult.setMessage("成功修改"+i+"条数据");
            return responseResult;
        }else{
            responseResult.setCode(500);
            responseResult.setMessage("修改失败，请重试");
            return responseResult;
        }
    }

    //通过id寻找课程
    @RequestMapping("/findcoursebyid")
    public ResponseResult<Course> findCourseById(int id){
        Course course = courseService.findCourseByid(id);
        ResponseResult<Course> responseResult=new ResponseResult<>();
        if(course!=null){
            responseResult.setCode(200);
            responseResult.setMessage("success");
            responseResult.setData(course);
            return responseResult;
        }else{
            responseResult.setCode(500);
            responseResult.setMessage("没有找到课程，请稍后重试");
            return responseResult;
        }
    }



    @RequirePermission("course:find")
    @GetMapping("/findcoursebyuid")
    @ApiOperation(value = "根据用户查询课程",notes = "返回课程")
    public ResponseResult<List> findCourseByUid(HttpServletRequest request){
        User user = GetUserUtil.getUser(request);
        List<Course> courses = courseService.findCourseByUid(user.getId());
        ResponseResult<List> responseResult=new ResponseResult<>();
        if(courses!=null){
            responseResult.setCode(200);
            responseResult.setMessage("success");
            responseResult.setData(courses);
            return responseResult;
        }else{
            responseResult.setCode(500);
            responseResult.setMessage("没有找到课程，请稍后重试");
            return responseResult;
        }
    }
    @GetMapping("/findcoursebyuser")
    public ResponseResult<Course> findCourseByUser(HttpServletRequest request){
        User user = GetUserUtil.getUser(request);
        Course course = courseService.findCourseByUser(user);
        ResponseResult<Course> responseResult=new ResponseResult<>();
        if(course!=null){
            responseResult.setCode(200);
            responseResult.setMessage("success");
            responseResult.setData(course);
            return responseResult;
        }else{
            responseResult.setCode(500);
            responseResult.setMessage("没有找到课程，请稍后重试");
            return responseResult;
        }
    }

    @GetMapping("/findcoursebytid")
    @ApiOperation(value = "根据用户查询课程",notes = "返回课程")
    public ResponseResult<List> findCourseBytid(HttpServletRequest request){
        User user = GetUserUtil.getUser(request);
        List<Course> courses = courseService.findCourseBytid(user.getId());
        Teacher teacher = teacherService.findById(user.getId());
        for (Course cours : courses) {
            cours.setTeacher(teacher);
        }
        ResponseResult<List> responseResult=new ResponseResult<>();
        if(courses!=null){
            responseResult.setCode(200);
            responseResult.setMessage("success");
            responseResult.setData(courses);
            return responseResult;
        }else{
            responseResult.setCode(500);
            responseResult.setMessage("没有找到课程，请稍后重试");
            return responseResult;
        }
    }
    //通过课程ID查章节
    @PostMapping("/findchaptersbycourse")
    public ResponseResult<List<Chapter>> findChaptersByCourse(@RequestBody Course course){

        int courseid = course.getId();
        ResponseResult<List<Chapter>> responseResult=new ResponseResult<>();
        List<Chapter> chapters = courseService.findChaptersByCourseId(courseid);
        if(chapters!=null){
            responseResult.setData(chapters);
            responseResult.setMessage("sucess");
            responseResult.setCode(200);
            return responseResult;
        }else{
            responseResult.setData(null);
            responseResult.setMessage("未找到相关数据");
            responseResult.setCode(500);
            return responseResult;
        }

    }

    @GetMapping("/findchaptersbycourseid")
    public ResponseResult<List<Chapter>> findChaptersByCourse(HttpServletRequest request){
        int courseid = Integer.parseInt((String)request.getParameter("courseid"));
        ResponseResult<List<Chapter>> responseResult=new ResponseResult<>();
        List<Chapter> chapters = courseService.findChaptersByCourseId(courseid);
        if(chapters!=null){
            responseResult.setData(chapters);
            responseResult.setMessage("sucess");
            responseResult.setCode(200);
            return responseResult;
        }else{
            responseResult.setData(null);
            responseResult.setMessage("未找到相关数据");
            responseResult.setCode(500);
            return responseResult;
        }

    }
    @PostMapping("/addchapter")
    public ResponseResult<List> addchapter(@RequestBody Map<String,Object> map, HttpServletRequest request){
        Object dynamicItem = map.get("dynamicItem");
        Object course = map.get("course");
        Map<String,Object> map2=(Map<String, Object>) course;
        String name = (String) map2.get("name");
        int id = GetUserUtil.getUser(request).getId();
        Course course1=courseService.findCourseByName(name,id);
        int courseId = course1.getId();
        List list=(List) dynamicItem;
        for (Object o : list) {
            Map<String,String> map1=(Map<String, String>) o;
            Chapter chapter = new Chapter().setChaptername(map1.get("name")).setCourseid(courseId);
            int i=chapterService.addChapter(chapter);
            if(i!=1){
                return new ResponseResult<>(500, "添加失败", null);
            }
        }
        List chapterlist=chapterService.findChapterByCourseId(courseId);
        /*if (chapterlist != null) {
            return new ResponseResult<>(500, "该课程已存在", null);
        }*/
        return new ResponseResult<>(200, "添加成功", chapterlist);
    }

    @PostMapping("/addsection")
    public ResponseResult<List> addsection(@RequestBody Map<String,Object> map,HttpServletRequest request){
        Object dynamicItem = map.get("dynamicItem");
        Object coursechapter = map.get("coursechapter");
        ArrayList chaptermap=(ArrayList) coursechapter;
        ArrayList<Integer> idlist=new ArrayList();
        for (Object o : chaptermap) {
            Map<String,Integer> map1=(Map<String, Integer>) o;
            idlist.add(map1.get("id"));
        }

        for (int i = 0; i < idlist.size(); i++) {
            Section section=new Section().setChaptid(idlist.get(i));
            System.out.println(section);
        }
        return null;
    }

    @PostMapping("/findteacherbycourse")
    @ApiOperation(value = "查询教师",notes = "返回老师信息")
    public ResponseResult<Teacher> findTeacherByCourse(@RequestBody Course course){
        int id = course.getTeacherid();
        Teacher teacher = teacherService.findById(id);

        if (teacher != null) {
            return new ResponseResult<>(200,"查询成功",teacher);
        }else{
            return new ResponseResult<>(500,"教师信息不存在，请添加",null);
        }
    }

    @RequestMapping("/findCourseFrontList")
    public ResponseResult<PageInfo<Course>> findCourseFrontList(HttpServletRequest request, @RequestBody CourseQuery courseQuery) {
        ResponseResult responseResult = new ResponseResult();

        try {
            PageInfo<Course> pageInfo = this.courseService.getCourseFrontlist(courseQuery);
            responseResult.setData(pageInfo);
            responseResult.setMessage("success");
            responseResult.setCode(200);
        } catch (Exception e) {
            responseResult.setCode(500);
            responseResult.setMessage("获取商品；列表失败");
            e.printStackTrace();
        }

        return responseResult;
    }

    //通过课程ID查询参考书
    @RequestMapping("/findreferencesbycourseid")
    public ResponseResult<List<Reference>> findReferencesByCourseId(Course course, HttpServletRequest request) {
        int courseid = course.getId();
        ResponseResult<List<Reference>> responseResult=new ResponseResult<>();
        List<Reference> references = courseService.findReferencesByCourceId(courseid);
        if(references!=null){
            responseResult.setData(references);
            responseResult.setMessage("sucess");
            responseResult.setCode(200);
            return responseResult;
        }else{
            responseResult.setData(null);
            responseResult.setMessage("未找到相关数据");
            responseResult.setCode(500);
            return responseResult;
        }

    }

    //通过课程考核ID（assessmentid)查询考核标准
    @PostMapping("/findassessmentbyid")
    public ResponseResult<Assessment> findAssessmentById(@RequestBody Course course) {

        int assessmentId = course.getAssessmentid();
        Assessment assessment = courseService.findAssessmentById(assessmentId);
        ResponseResult<Assessment> responseResult=new ResponseResult<>();
        if(assessment!=null){
            responseResult.setData(assessment);
            responseResult.setMessage("sucess");
            responseResult.setCode(200);
            return responseResult;
        }else{
            responseResult.setData(null);
            responseResult.setMessage("未找到相关数据");
            responseResult.setCode(500);
            return responseResult;
        }

    }
}
